package cn.edu.neusoft.zengweidong707.neufood.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.bean.IsSuccessBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ZwdListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;

public class RegActivity extends BaseActivity {
    private EditText username;
    private EditText userpass;
    private EditText realpass;
    private EditText mobilenum;
    private EditText address;
    private EditText comment;
    private Button btn_reg;
    private Button btn_return;
    private ZwdListener<IsSuccessBean> listener;
    private UserModel model;
    private Intent intent;

    @Override
    void initView() {
        setContentView(R.layout.activity_reg);
        username = (EditText)findViewById(R.id.et_username);
        userpass = (EditText)findViewById(R.id.et_userpass);
        realpass = (EditText)findViewById(R.id.et_realpass);
        mobilenum = (EditText)findViewById(R.id.et_mobilenum);
        address = (EditText)findViewById(R.id.et_address);
        comment = (EditText)findViewById(R.id.et_comment);
        btn_reg = (Button)findViewById(R.id.btn_reg);
        btn_return = (Button)findViewById(R.id.btn_return);
    }

    @Override
    void initEvent() {
        intent = new Intent(RegActivity.this,LoginActivity.class);
        listener = new ZwdListener<IsSuccessBean>() {
            @Override
            public void onResponse(IsSuccessBean l) {
                if(l.getSuccess().equals("0")) {
                    showToast("注册失败, 用户名已存在");
                } else {
                    showToast("注册成功，返回登录");
                    intent.putExtra("name", username.getText().toString());
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onFail(String msg) {
                showToast(msg);
            }
        };

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.getText().toString().isEmpty() || userpass.getText().toString().isEmpty()
                        || realpass.getText().toString().isEmpty() || mobilenum.getText().toString().isEmpty()
                        || address.getText().toString().isEmpty() || comment.getText().toString().isEmpty()) {
                    showToast("信息不能为空");
                } else {
                    if(!realpass.getText().toString().equals(userpass.getText().toString())) {
                        showToast("密码前后不一致");
                    } else {
                        model = new UserModel();
                        model.reg(username.getText().toString(), userpass.getText().toString(),
                                mobilenum.getText().toString(), address.getText().toString(),
                                comment.getText().toString(), listener);
                    }
                }
            }
        });
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model=new UserModel();
                startActivity(intent);
            }
        });
    }

    @Override
    void initData() {

    }

}
